package appnode;

import javax.jdo.PersistenceManager;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.List;
import java.util.ArrayList;
import appnode.Node;
import appnode.Ratings;
import appnode.SearchNode;
import appnode.Link;
import javax.jdo.Query;
import appnode.PMF;

public class RateUtils {
	
	public static Ratings getRatingByOwner (String name) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ratings.class);
		query.setFilter("owner==ownerParam");
		query.declareParameters("String ownerParam");
		List<Ratings> results = (List<Ratings>) query.execute(name);
		return results.get(0);
	}
	
	// string to Rate
	public static double stringtoRate(String rating) {
		double rate=0;
		if (rating.equals("1")){
			rate=1;
		}
		else if (rating.equals("2")){
			rate=2;
		}
		else if (rating.equals("3")){
			rate=3;
		}
		else if(rating.equals("4")){
			rate=4;
		}
		else if(rating.equals("5")){
			rate=5;
		}
		else if(rating.equals("select")){
			rate=-1;
		}
		return rate;
	}
	//get Rates
	public static double getcomRate(String nodename) {
		double rate=0;
		Node node = SearchNode.getByName(nodename);
		int size = node.getNoderatings().size();
		for (int i=0;i<size;i++){
			rate += node.getNoderatings().get(i).getCommun();
			}
		rate = rate/size;
	return rate;
	}
	
	public static double getcolRate(String nodename) {
		double rate=0;
		Node node = SearchNode.getByName(nodename);
		int size = node.getNoderatings().size();
		for (int i=0;i<size;i++){
			rate += node.getNoderatings().get(i).getCollabor();
			}
		rate = rate/size;
	return rate;
	}
	
	public static double getexpRate(String nodename) {
		double rate=0;
		Node node = SearchNode.getByName(nodename);
		int size = node.getNoderatings().size();
		for (int i=0;i<size;i++){
			rate += node.getNoderatings().get(i).getExpert();
			}
		rate = rate/size;
	return rate;
	}
	
		
	// noderate
	public static double addComrate(String nodename, String noderating, double comrating) {
		double comrate = 0;
		int i = 0;
		int j = 0;
		if (comrating==-1) {
			comrate = comrating;
		}
		else {
			Node node = SearchNode.getByName(nodename);
			for (i=0;i<node.getNoderatings().size();i++) {
				if (node.getNoderatings().get(i).getNodename().equals(noderating)) {
					comrate += node.getNoderatings().get(i).getCommun();
					j++;
				}
			}
				comrate = (comrate + comrating)/(j+1);
		}
			return comrate;
	}
		
	public static double addColrate(String nodename, String noderating, double colrating){
		double colrate = 0;
		int i = 0;
		int j = 0;
		if (colrating==-1) {
			colrate = colrating;
		} 
		else {
			Node node = SearchNode.getByName(nodename);
			for (i=0;i<node.getNoderatings().size();i++) {
				if (node.getNoderatings().get(i).getNodename().equals(noderating)) {
					colrate += node.getNoderatings().get(i).getCollabor();
					j++;
				}
			}
				colrate = (colrate + colrating)/(j+1);
		}
			return colrate;
	}
	
	public static double addExprate(String nodename, String noderating, double exprating){
		double exprate = 0;
		int i = 0;
		int j = 0;
		if (exprating==-1) {
			exprate = exprating;
		} 
		else {
			Node node = SearchNode.getByName(nodename);
			for (i=0;i<node.getNoderatings().size();i++) {
				if (node.getNoderatings().get(i).getNodename().equals(noderating)) {
					exprate += node.getNoderatings().get(i).getExpert();
					j++;
				}
			}
				exprate = (exprate + exprating)/(j+1);
		}
			return exprate;
	}
	
	public static double addComrateOwner(String ownername) {
		
		double comrate = -1;
		double rate = 0;
		int i = 0;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(NodeRatings.class);
		query.setFilter("owner==ownerParam");
		query.declareParameters("String ownerParam");
		List<NodeRatings> results = (List<NodeRatings>) query.execute(ownername);
		for (NodeRatings r : results) {
			if (r.getCommun()!=-1) {
				rate += r.getCommun();
				i++;	
			} // if
		} // for
		if (i>0) {
			rate = rate/i;
		}
		if (i>0) {
			comrate = rate;
		}
		return comrate;			
	}
	
	public static double addColrateOwner(String ownername) {
		
		double colrate = -1;
		double rate = 0;
		int i = 0;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(NodeRatings.class);
		query.setFilter("owner==ownerParam");
		query.declareParameters("String ownerParam");
		List<NodeRatings> results = (List<NodeRatings>) query.execute(ownername);
		for (NodeRatings r : results) {
			if (r.getCollabor()!=-1) {
				rate += r.getCollabor();
				i++;	
			} // if
		} // for
		if (i>0) {
			rate = rate/i;
		}
		if (i>0) {
			colrate = rate;
		}
		return colrate;			
	}
	
	public static double addExprateOwner(String ownername) {
		
		double exprate = -1;
		double rate = 0;
		int i = 0;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(NodeRatings.class);
		query.setFilter("owner==ownerParam");
		query.declareParameters("String ownerParam");
		List<NodeRatings> results = (List<NodeRatings>) query.execute(ownername);
		for (NodeRatings r : results) {
			if (r.getExpert()!=-1) {
				rate += r.getExpert();
				i++;	
			} // if
		} // for
		if (i>0) {
			rate = rate/i;
		}
		if (i>0) {
			exprate = rate;
		}
		return exprate;			
	}
	
}